<?php
/**
 * Bean holding page requested information
 */
class RequestPage {
	private $strRequestMethod;
	private $strQueryString;
	private $strContext;
	private $strURL;
	private $strURLPage;
	private $strURLExtension;
	private $strFullURL;
	
	/**
	 * Collects information from $_SERVER variables.
	 */
	public function __construct() {
		$this->setRequestMethod();
		$this->setContext();
		$this->setURL();
		$this->setFullURL();
		$this->setQueryString();
	}
	/**
	 * Sets the name of the HTTP method with which this request was made.
	 * 
	 * @return void
	 */
	private function setRequestMethod() {
		$this->strRequestMethod=$_SERVER["REQUEST_METHOD"];
	}
	
	/**
	 * Gets the name of the HTTP method with which this request was made
	 * 
	 * @example GET
	 * 
	 * @return string
	 */
	public function getRequestMethod() {
		return $this->strRequestMethod;
	}
	
	/**
	 * Sets the portion of the request URL that indicates the context of the request.
	 * 
	 * @return void
	 */
	private function setContext() {
		$this->strContext = str_replace(array($_SERVER["DOCUMENT_ROOT"],"/index.php"),"",$_SERVER["SCRIPT_FILENAME"]);
	}
	
	/**
	 * Gets the portion of the request URL that indicates the context of the request.
	 * 
	 * @example /servlets/
	 * 
	 * @return string
	 */
	public function getContext() {
		return $this->strContext;
	}
	
	/**
	 * Reads request URL for page and extension and saves both.
	 * 
	 * If page is not supplied, it is replaced with default page defined in XML as application>default_page.
	 * If extension is not supplied,  it is replaced with default extension defined in XML as application>default_extension.
	 * 
	 * @return void
	 */
	private function setURL() {
		$this->strURL = $_SERVER['SCRIPT_URL'];
		
		// split URL into page and extension
		$strURL = $_SERVER['SCRIPT_URL'];
		$strExtension = "";
		if(strpos($_SERVER['SCRIPT_URL'],".")!==false) {
			$strURL = substr($_SERVER['SCRIPT_URL'],0,strrpos($_SERVER['SCRIPT_URL'],"."));
			$strExtension = strtolower(substr($_SERVER['SCRIPT_URL'],(strrpos($_SERVER['SCRIPT_URL'],".")+1)));
		}
		$strURL = str_replace($this->strContext,"",$strURL);
		
		// write url page
		if($strURL!="/") {
			$this->strURLPage = $strURL;
		} else {
			$strDefaultPage = Servlet::$configuration->getAttribute("application.default_page");
			if(!$strDefaultPage) throw new ServletException("Configuration error: application.default_page not set");
			$this->strURLPage = $strDefaultPage;
		}
		
		// write url extension
		if($strExtension) {
			$this->strURLExtension = $strExtension;
		} else {
			$strDefaultExtension = Servlet::$configuration->getAttribute("application.default_extension");
			if(!$strDefaultExtension) throw new ServletException("Configuration error: application.default_extension not set");
			$this->strURLExtension = $strDefaultExtension;
		}
	}
	
	/**
	 * Gets page and extension from request URL
	 */
	public function getURL() {
		return $this->strURL;
	}
	
	/**
	 * Gets page requested by client.
	 * 
	 * @example test
	 * 
	 * @return string
	 */
	public function getURLPage() {
		return $this->strURLPage;
	}
	
	/**
	 * Gets extension for the page requested by client.
	 * 
	 * @example html
	 * 
	 * @return string
	 */
	public function getURLExtension() {
		return $this->strURLExtension;
	}
	
	/**
	 * Sets full request url.
	 * 
	 * @return void
	 */
	private function setFullURL() {
		$this->strFullURL = $_SERVER['SCRIPT_URI'];
	}
	
	/**
	 * Gets full request url.
	 * 
	 * @example http://www.test.com/servlets/test.html
	 * 
	 * @return string
	 */
	public function getFullURL() {
		return $this->strFullURL;
	}
	
	/**
	 * Sets request QUERY STRING.
	 * 
	 * @return void
	 */
	private function setQueryString() {
		$this->strQueryString = $_SERVER["QUERY_STRING"];
	}
	
	/**
	 * Gets request QUERY STRING
	 * 
	 * @example a=1&b=2
	 * 
	 * @return string
	 */
	public function getQueryString() {
		return $this->strQueryString();
	}

	/**
	 * Outputs collected information as associative array.
	 *
	 * @return array
	 */
	public function toArray() {		
		$tblOutput = array();
		$tblOutput['query_string'] = $this->strQueryString;
		$tblOutput['request_method'] = $this->strRequestMethod;
		$tblOutput['url']['context'] = $this->strContext;
		$tblOutput['url']['page'] = $this->strURLPage;
		$tblOutput['url']['extension'] = $this->strURLExtension;
		$tblOutput['url']['full'] = $this->strFullURL;
		return $tblOutput;
	}
}